package com.cy.mall.model.enums;

import lombok.Getter;

import java.util.HashMap;
import java.util.Map;
import java.util.stream.Stream;

import com.cy.mall.model.enums.order.OrderStatus;

/**
 * @description: 产品流程
 * @author: Ocean
 * @create: 2018-07-20 17:15
 **/
public enum Process {

    APPLY(OrderStatus.APPLY_SUCCESS, null, 0, "申请", 0),
    FIRST_AUDIT(OrderStatus.FIRST_AUDIT_SUCCESS, OrderStatus.FIRST_AUDIT_FAIL, 1, "初审", 1),
    FACE_SIGN(OrderStatus.FACE_SIGN_SUCCESS, OrderStatus.FACE_SIGN_FAIL, 2, "面签", 2),
    CALL_VERIFY(OrderStatus.CALL_VERIFY_SUCCESS, OrderStatus.CALL_VERIFY_FAIL, 3, "电核", 3),
    REVIEW(OrderStatus.REVIEW_SUCCESS, OrderStatus.REVIEW_FAIL, 0, "复议", 4),
    LOAN(OrderStatus.LOAN_SUCCESS, OrderStatus.LOAN_FAIL, 4, "放款", 5),
    REVIEW_APPLY(OrderStatus.REVIEW_WAIT, null, 0, "复议申请", 6),
    ;

    @Getter
    private OrderStatus pass;

    @Getter
    private OrderStatus reject;

    @Getter
    private Integer order;

    @Getter
    private String remark;
    
    @Getter
    private Integer arrOrder;

    static final Map<OrderStatus, Process> lastProcessMap = new HashMap<>();
    static final Map<OrderStatus, Process> failMap = new HashMap<>();
    static final Map<String, Process> nameMap = new HashMap<>();

    static {
        //所有通过状态
        Stream.of(Process.values()).forEach(p -> {
            nameMap.put(p.name(), p);
            lastProcessMap.put(p.getPass(), p);
            failMap.put(p.getReject(), p);
        });
        //电核失败，找到上一步流程为电核
        lastProcessMap.put(OrderStatus.CALL_VERIFY_FAIL, CALL_VERIFY);
        lastProcessMap.put(OrderStatus.REVIEW_SUCCESS, CALL_VERIFY);
    }

    Process(OrderStatus pass, OrderStatus reject, Integer order, String remark, Integer arrOrder) {
        this.pass = pass;
        this.reject = reject;
        this.order = order;
        this.remark = remark;
        this.arrOrder = arrOrder;
    }

    /**
     * 根据当前审核状态获取上一步审核流程
     * @param orderStatus
     * @return
     */
    public static Process getProcess(OrderStatus orderStatus) {
        return lastProcessMap.get(orderStatus);
    }

    /**
     * 根据流程名称查找流程
     * @param name
     * @return
     */
    public static Process getProcess(String name){
        return nameMap.get(name);
    }

    /**
     * 失败的时候，根据失败状态获取当前失败的审核流程
     * @param orderStatus
     * @return
     */
    public static Process getProcessByFailStatus(OrderStatus orderStatus) {
        return failMap.get(orderStatus);
    }

}
